﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Script.Services;

namespace NghiSonStyle
{
    public partial class Test : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            loadDropdownlist();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            //Get Filename from fileupload control
            string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
            string imName = Name.Text;
            string a = Idimput.Text;

            //Save images into Images folder
            fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
            //Getting dbconnection from web.config connectionstring
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
            //Open the database connection
            con.Open();
            //Query to insert images path and name into database
            SqlCommand cmd = new SqlCommand("Insert into ImagesPath(Kieu,ImageName,ImagePath) values(@Kieu,@ImageName,@ImagePath)", con);
            //Passing parameters to query
            cmd.Parameters.AddWithValue("@Kieu", a);
            cmd.Parameters.AddWithValue("@ImageName", imName);
            cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
            cmd.ExecuteNonQuery();
            //Close dbconnection
            con.Close();
            Response.Redirect("~/Test.aspx");
        }
        [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public static List<ImageEntity> GetCurrentTime(string name)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("Select ImagePath,ImageName,Kieu from ImagesPath ORDER BY Kieu", con);
            //SqlCommand cmdA = new SqlCommand("Select name from AddMenu where Lop = 'A' ", con);
            //SqlCommand cmdB = new SqlCommand("Select name from AddMenu where Lop = 'B' ", con);
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            con.Close();
            List<ImageEntity> list = new List<ImageEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                list.Add(new ImageEntity(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString()));
            }
            return list;
        }
        [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public static List<Layerlist> GetLayer(string name)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
            con.Open();
           // SqlCommand cmd = new SqlCommand("Select ImagePath,ImageName,Kieu,name from ImagesPath,AddMenu ORDER BY Kieu", con);
            SqlCommand cmdA = new SqlCommand("Select name from AddMenu ", con);
            //SqlCommand cmdB = new SqlCommand("Select name from AddMenu where Lop = 'B' ", con);
            DataTable dt1 = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmdA);
            da.Fill(dt1);
            con.Close();
            List<Layerlist> list = new List<Layerlist>();
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                list.Add(new Layerlist(dt1.Rows[i][0].ToString()));
            }
            return list;
        }

        public class ImageEntity
        {
            public string ImagePath { get; set; }
            public string ImageName { get; set; }
            
            //public string LayerNameB { get; set; }
            public ImageEntity(string ImagePath, string ImageName)
            {
                this.ImagePath = ImagePath;
                this.ImageName = ImageName;
                
            }
        }
        public class Layerlist
        {
            
            public string LayerNameA { get; set; }
            //public string LayerNameB { get; set; }
            public Layerlist(string LayerNameA)
            {          
                this.LayerNameA = LayerNameA;
            }
        }
        public void loadDropdownlist()
        {
           
            
            con.Open();
            SqlCommand cmd = new SqlCommand("Select Name from AddMenu", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            DataRow dh = dt.NewRow();          
            dt.Rows.Add(dh);
            da.Fill(dt);
            con.Close();
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "Name";
           // DropDownList1.DataValueField = "Name";
            DropDownList1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            //Get Filename from fileupload control
            string LayerName = txtLayerName.Text;
            string Layertype = txtLayerType.Text;  
            //Open the database connection
            con.Open();
            //Query to insert images path and name into database
            SqlCommand cmd = new SqlCommand("Insert into AddMenu(Name,Lop) values(@Name,@Lop)", con);
            //Passing parameters to query
            cmd.Parameters.AddWithValue("@Name", LayerName);
            cmd.Parameters.AddWithValue("@Lop", Layertype);
            cmd.ExecuteNonQuery();
            //Close dbconnection
            con.Close();
            Response.Redirect("~/Test.aspx");
            Label6.Text = "Thêm lớp thành công";
        }


    }
}